﻿using System;
using UnityEngine;
using UnityTouchTable.Runtime;

namespace UnityTouchTable.Example
{
    /// <summary>
    /// 标记效果
    /// </summary>
    public class MarkerItem : MarkerItemBase
    {
        #region Field
        /// <summary>
        /// 中心节点
        /// </summary>
        [SerializeField]
        private CenterNode m_CenterNode = default;

        /// <summary>
        /// 角度均分器
        /// </summary>
        [SerializeField]
        private AngleEqualizer m_AngleEqualizer = default;

        /// <summary>
        /// 子内容节点
        /// </summary>
        [SerializeField]
        private SubContentNodeBase[] m_SubContentNodes = default;

        /// <summary>
        /// 当前显示的子节点内容
        /// </summary>
        private SubContentNodeBase m_CurrentSubContentNode;
        #endregion

        #region Property
        /// <summary>
        /// 获取或设置当前显示的子节点内容
        /// </summary>
        /// <value></value>
        public SubContentNodeBase CurrentSubContentNode
        {
            get => this.m_CurrentSubContentNode;
            set => this.m_CurrentSubContentNode = value;
        }
        #endregion

        #region Function
        public override void AddMarker(Vector2 centerPosition, float initAngle)
        {
            this.m_AngleEqualizer.Count = this.m_SubContentNodes.Length;
            this.m_AngleEqualizer.ChangeAngleIndex += this.OnChangeAngleIndex;
            this.m_CurrentSubContentNode = this.m_SubContentNodes[0];
            this.m_CurrentSubContentNode.Show(this.m_CenterNode.Randius);

            base.AddMarker(centerPosition, initAngle);
        }

        public override void RemoveMarker()
        {
            this.m_AngleEqualizer.ChangeAngleIndex -= this.OnChangeAngleIndex;
            this.m_CurrentSubContentNode.Hide(true);

            base.RemoveMarker();
        }

        /// <summary>
        /// 当角度改变时
        /// </summary>
        /// <param name="sender">发送者</param>
        /// <param name="e">事件参数</param>
        private void OnChangeAngleIndex(object sender, EventArgs e)
        {
            ChangeAngleIndexEventArgs changeAngleIndexEventArgs = e as ChangeAngleIndexEventArgs;
            if (changeAngleIndexEventArgs == null)
                return;

            SubContentNodeBase subContentNode = this.m_SubContentNodes[changeAngleIndexEventArgs.AngleIndex];
            //if (this.m_CurrentSubContentNode = subContentNode)
            //    return;

            this.m_CurrentSubContentNode.Hide();
            this.m_CurrentSubContentNode = subContentNode;
            this.m_CurrentSubContentNode.Show(this.m_CenterNode.Randius, changeAngleIndexEventArgs.UnitAngle * changeAngleIndexEventArgs.AngleIndex);
        }
        #endregion
    }
}